
//
//  HMEmoticonKeyBoard.swift
//  微博
//
//  Created by 程双岐 on 16/10/6.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit


private let CellId = "cellID"

class HMEmoticonKeyBoard: UIView {

    
    
    fileprivate lazy var pageControl: UIPageControl = {
       let page = UIPageControl()
        page.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
        page.pageIndicatorTintColor =  UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
        page.hidesForSinglePage = true
        
        return page
    }()
    
    @available(iOS 9.0, *)
    fileprivate lazy var toolBar: HMEmoticonToolBar = HMEmoticonToolBar()
    
    fileprivate lazy var emoticonCollectionView: UICollectionView = {
        let flowLatout = UICollectionViewFlowLayout()
        flowLatout.scrollDirection = .horizontal
        
        let view = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLatout)
        view.backgroundColor = self.backgroundColor
        view.isPagingEnabled = true
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.bounces = false
        view.dataSource = self
        view.delegate = self
        
        view.register(HMEmoticonCollectionViewCell.self, forCellWithReuseIdentifier: CellId)
        
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {
        
        
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        let normalIndexPath = IndexPath(item: 0, section: 1)
        
        DispatchQueue.main.async {
            self.emoticonCollectionView.scrollToItem(at: normalIndexPath, at: .left, animated: false)
        }
        setPageControlData(indexPath: normalIndexPath)
        if #available(iOS 9.0, *) {
            addSubview(toolBar)
            addSubview(emoticonCollectionView)
            addSubview(pageControl)
        } else {
            // Fallback on earlier versions
        }
        if #available(iOS 9.0, *) {
            emoticonCollectionView.snp_makeConstraints(closure: { (make) in
                make.top.equalTo(self)
                make.left.equalTo(self)
                make.right.equalTo(self)
                make.bottom.equalTo(toolBar.snp_top)
            })
            pageControl.snp_makeConstraints { (make) in
                make.bottom.equalTo(emoticonCollectionView)
                make.centerX.equalTo(emoticonCollectionView)
                make.height.equalTo(10)
            }
            
            toolBar.snp_makeConstraints { (make) in
                make.bottom.equalTo(self)
                make.left.equalTo(self)
                make.right.equalTo(self)
                make.height.equalTo(35)
            }
        } else {
            // Fallback on earlier versions
        }
        if #available(iOS 9.0, *) {
            toolBar.callBack = { [weak self] (type: HMEmoticonToolBarButtonType) in
                let indexPath: IndexPath
                
                
                switch type {
                case .recent:
                    print("最近")
                    indexPath = IndexPath(item: 0, section: 0)
                case .normal:
                    print("mor")
                    indexPath = IndexPath(item: 0, section: 1)

                case .emoji:
                    print("emoji")
                    indexPath = IndexPath(item: 0, section: 2)

                case .lxh:
                    print("lxh")
                    indexPath = IndexPath(item: 0, section: 3)

                }
                self?.emoticonCollectionView.scrollToItem(at: indexPath, at: .left, animated: false)
                self?.setPageControlData(indexPath: indexPath)

            }
        } else {
            // Fallback on earlier versions
        }
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let flowLayout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        
        
        flowLayout.itemSize = emoticonCollectionView.size
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        
        
    }
    
    func setPageControlData(indexPath: IndexPath){
        pageControl.numberOfPages = HMEmoticonTools.shareTools.allEmoticonsArray[indexPath.section].count
        pageControl.currentPage = indexPath.item
    }
    
    func reloadRecentData() {
        let indexPath = IndexPath(item: 0, section: 0)
        
        emoticonCollectionView.reloadItems(at: [indexPath])
    }
    
}

extension HMEmoticonKeyBoard: UICollectionViewDataSource , UICollectionViewDelegate{

    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return HMEmoticonTools.shareTools.allEmoticonsArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return HMEmoticonTools.shareTools.allEmoticonsArray[section].count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellId, for: indexPath) as! HMEmoticonCollectionViewCell
        cell.emoticonArray = HMEmoticonTools.shareTools.allEmoticonsArray[indexPath.section][indexPath.item]
//        cell.backgroundColor = RandomColor()
        cell.indexPath = indexPath
        return cell
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let centerX = scrollView.contentOffset.x + emoticonCollectionView.width / 2
        let centerY = scrollView.contentOffset.y + emoticonCollectionView.height / 2
        
        let center = CGPoint(x: centerX, y: centerY)
        
        if let indexPath = emoticonCollectionView.indexPathForItem(at: center){
            let section = indexPath.section
            

            if #available(iOS 9.0, *) {
                toolBar.seleccedButton(section: section)
                setPageControlData(indexPath: indexPath)
            } else {
                // Fallback on earlier versions
            }

        }
        
    }
    
    
    
}


